package com.qef.com.mmovie.base;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;

import com.qef.com.mmovie.App;
import com.qef.com.mmovie.R;
import com.qef.com.mmovie.widget.ProgressDialogF;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 2016 逛街购（北京）网络科技有限公司，版权所有
 * guangjiegou - Android客户端
 * Context:activity基类
 * Created by fanenqian on 2017/4/24 18:00
 */

public abstract class BaseActivity<T extends BasePresenter> extends RxAppCompatActivity implements BaseView {
    @Inject
    protected T mPresenter;
    protected View mView;
    protected Context mContext;
    private Unbinder mUnBinder;
    protected boolean isInited = false;
    private ProgressDialogF mProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initInject(getActivityComponent());
        mContext = this;
        setContentView(getLayoutId());
        if (useEventBus())//如果要使用eventbus请将此方法返回true
//            EventBus.getDefault().register(this);//注册到事件主线
            mPresenter.attachView(this);
        mUnBinder = ButterKnife.bind(this);
        //依赖注入
        App.getInstance().addActivity(this);
        initEventAndData();
    }

    protected abstract int getLayoutId();

    protected abstract void initEventAndData();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
        if (mUnBinder != Unbinder.EMPTY) mUnBinder.unbind();
        if (useEventBus())//如果要使用eventbus请将此方法返回true
            EventBus.getDefault().unregister(this);
        this.mPresenter = null;
        this.mUnBinder = null;
        this.mContext = null;
        App.getInstance().removeActivity(this);
        if (mPresenter != null) mPresenter.detachView();
    }

    /**
     * activity 注入对象
     *
     * @return
     */
    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .build();
    }

    /**
     * 是否使用eventBus,默认为使用(true)，
     *
     * @return
     */
    protected boolean useEventBus() {
        return true;
    }

    /**
     * 初始化进度款，子类需要用到进度款的时候条用此方法，不需要的忽略
     */
    protected void initProgress() {
        //初始化网络请求进度款
        mProgressDialog = new ProgressDialogF(mContext);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
//                    moveTaskToBack(false);
                    mProgressDialog.dismiss();
                }
                return true;
            }
        });
    }

    /**
     * 显示进度框
     *
     * @param msg 提示信息
     */
    protected void showProgress(String msg) {
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
            mProgressDialog.setMessage(msg);
        } else {
            initProgress();
            mProgressDialog.show();
            mProgressDialog.setMessage(msg);
        }
    }

    /**
     * 移出进度款
     */
    protected void cancleProgress() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    protected abstract void initInject(ActivityComponent activityComponent);

    protected void initToolbarNav(Toolbar toolbarx, String title) {
        toolbarx.setNavigationIcon(R.mipmap.ic_arrow_back_white_24dp);
        toolbarx.setNavigationOnClickListener(v -> onBackPressed());
        toolbarx.setTitle(title);
        toolbarx.setTitleTextColor(Color.WHITE);
    }
}
